(:
  A procedure to select the contents of an atom xml feed
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
declare namespace atom = "http://www.w3.org/2005/Atom";

<display>{
let $t := $arguments[1] 
let $startdate := concat($arguments[2],"T00:00:00")
let $enddate := concat($arguments[3],"T23:59:59")
let $d := doc($t)/atom:feed/atom:entry

let $posts := for $entry at $p in $d
where $entry/atom:category[@scheme="http://schemas.google.com/g/2005#kind"][@term="http://schemas.google.com/blogger/2008/kind#post"]
return $entry
for $entry at $p in $posts
let $date := xs:dateTime($entry/atom:published/text())
let $contents := jj:remove-html($entry/atom:content/text())
where $date <= xs:dateTime($enddate) and $date >= xs:dateTime($startdate)
return  
	for $i in (0 to (fn:string-length($contents) idiv 2000))
	let $start := 2000 * $i + 1
	let $content := fn:substring($contents,$start, $start+2000)
	return
  <item>{
    <info name="time"><time><date>{xs:string(jj:date-to-millis($date))}</date><duration>0</duration></time></info>,
    <info name="date">{fn:substring(jj:editDate($date),1,10)}</info>,
    <info name="type">post</info>,
    <info name="src-anchor">{
    <anchor>{
        <doc>{ $t }</doc>,
        <path>{$entry/atom:id/text()}</path>
      }</anchor>
    }</info>,
    <info name="id">{xs:string($entry/atom:id)}</info>,
    <info name="author">{$entry/atom:author/atom:name/text()}</info>,
    <info name="title">{$entry/atom:title/text()}</info>,
    <info name="content">{$content}</info>,
    <info name="link">{xs:string($entry/atom:link[@rel="alternate"][@type="text/html"]/@href)}</info>
  }</item>
}</display>
